{include file="public/header" /}
<div class="page">
<!--    display:none;-->
    <form action="" method="post">
    <div class="list" style='margin-left: 20px;' id="allsa">
        <input style="margin-right: 8px;" type="checkbox" id="checkboxs" name="checkall">全选
        <?php foreach($top_area as $k=>$v){ ?>
        <div class="top">

    <span style="font-family: 'dtreefont' !important;font-size: 16px;font-style: normal;-webkit-font-smoothing: antialiased;-webkit-tap-highlight-color: rgba(0,0,0,0);"
          class='select_choose dtreefont dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon' data-spread="close">
    </span>
            <!-- name="checkOne[]" -->
            <input {if strpos($detail.area_id,$v['last_ids']) !== false}checked {/if} style="margin-right: 8px;" type="checkbox" value="<?php echo $v['area_id']; ?>" class="top"
                   area_deep="1" ids="<?php echo $v['last_ids']; ?>" data_name="<?php echo $v['area_name']; ?>"><?php echo $v['area_name']; ?>
            <div class="sec<?php echo $v['area_id']; ?>" style='padding-left:20px; margin:10px;'></div>
        </div>
        <?php }?>
    </div>
    <div class="bottom tc hr32">
        <input type="button" class="submit" value="提交" id='button_submit' />
    </div>
    </form>
</div>
<script src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js"></script>
<script>
    // alert(); // true
    $('#all').click(function () {
        if ($(this).prop('checked')) {
            $(".bd-line").find('input:checkbox').prop("checked", true);
        } else {
            $(".bd-line").find('input:checkbox').prop("checked", false);
        }
    });

    //数组去重
    function unique(arr) {
        var ret = []

        for (var i = 0; i < arr.length; i++) {
            var item = arr[i]
            if (ret.indexOf(item) === -1) {
                ret.push(item)
            }
        }

        return ret
    }


    // 获得选中ID
    function getItems() {
        /* 获取选中的项 */
        var items = '';
        $('.checkall:checked').each(function () {
            items += this.value + ',';
        });
        if (items != '') {
            items = items.substr(0, (items.length - 1));
        } else {
            layer.alert('请选择要操作的数据', {icon: 2})
        }
        return items;
    }


    //全选
    $(document).on('click', '#checkboxs', function () {
        if ($(this).prop('checked')) {
            $(".top").find('input:checkbox').prop("checked", true);
        } else {
            $(".top").find('input:checkbox').prop("checked", false);
        }

    });


    //单个操作设置销售区域
    $(document).on('click', '.save_area', function () {
        ids_str = $(this).attr('id');
        var url = $(this).attr('action');
        $(this).next().empty();
        id = $(this).parent().parent().attr('data-id');

        var input  = $('.list').find('input').each(function(){
            $(this).attr('class','aaa');
        });
        var $list = $('.list').html();
        $('.list').find('input').each(function () {
            $(this).attr('class', '');
        });

        layer.confirm('', {
            title: '销售区域选择',
            type: 1,
            area: ['900px', '750px'],
            fixed: false,
            maxmin: true,
            btn: ['确认', '取消'],
            content: $list,
        }, function () {
            var chk_values = [];
            var data_names = [];
            $('input[type="checkbox"]:checked').each(function () {
                if ($(this).attr('class') != 'aaa') {
                    return true;
                }
                chk_values.push($(this).attr('ids'));
                data_names.push($(this).attr('data_name'));
            });
            var area_name = unique(data_names);
            var chk_value = unique(chk_values);
            if (chk_value == '') {
                layer.closeAll();
            } else {
                $.ajax({
                    type: "POST",
                    url: url,
                    data:{ids:ids_str,area_name:area_name,area_ids:chk_value},
                    error: function (request) {
                        alert("Connection error");
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            layer.closeAll();
                            layer.msg('设置销售区域成功');
                            parent.location.reload();
                            return false;
                        } else {
                            layer.closeAll();
                            layer.msg('设置销售区域失败');
                            parent.location.reload();
                            return false;
                        }
                    }
                });
                $(".top").find('input:checkbox').prop("checked", false);
            }
        });
    });


    //展开城市
    $(document).on('click', '.select_choose', function () {
        var choose_id = $(this).next().val();
        var obj = $(this);
        var str111 = "{:$detail['area_id']}";
        $.ajax({
            type: 'POST',
            url: "{:url('Ziwork/ajax_area')}",
            cache: false,
            data: {choose_id: choose_id},
            dataType: 'json',
            success: function (data) {

                var htmls = '';
                $.each(data, function (k, v) {
                    if (v.area_deep != 3) {
                        if (str111.indexOf(v.last_ids) != -1)
                        {
                            htmls += '<span class="select_choose select_choose dtreefont dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon"></span><input checked="checked" type="checkbox"  area_deep="' + v.area_deep + '" value="' + v.area_id + '" ids="' + v.last_ids + '" data_name="' + v.top_name + '" class="aaa" > ' + v.area_name + ' <div class="last' + v.area_id + '" style="padding-left:20px; margin:10px;"></div>';
                        }else{
                            htmls += '<span class="select_choose select_choose dtreefont dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon"></span><input  type="checkbox"  area_deep="' + v.area_deep + '" value="' + v.area_id + '" ids="' + v.last_ids + '" data_name="' + v.top_name + '" class="aaa" > ' + v.area_name + ' <div class="last' + v.area_id + '" style="padding-left:20px; margin:10px;"></div>';
                        }
                    } else {
                        if (str111.indexOf(v.last_ids) != -1) {
                            htmls += '<i class="dtree-icon-shuye1 dtreefont-special dtree-theme-dtreefont dtree-theme-icon"></i>' + '<input type="checkbox"  checked="checked" id="p3"  ids="' + v.area_id + '"    area_deep="' + v.area_deep + '" data_name="' + v.top_name + '" class="aaa" >' + v.area_name;
                        }else{
                            htmls += '<i class="dtree-icon-shuye1 dtreefont-special dtree-theme-dtreefont dtree-theme-icon"></i>' + '<input type="checkbox" id="p3"  ids="' + v.area_id + '"    area_deep="' + v.area_deep + '" data_name="' + v.top_name + '" class="aaa" >' + v.area_name;
                        }
                    }
                });
                if ($(".sec" + choose_id).length > 0) {
                    $(".sec" + choose_id).html(htmls);
                    obj.attr("class", "area_hidden dtree-theme-dtreefont dtree-theme-ficon dtreefont dtree-icon-jian");
                } else {
                    $(".last" + choose_id).html(htmls);
                    obj.attr("class", "area_hidden dtree-theme-dtreefont dtree-theme-ficon dtreefont dtree-icon-jian");
                }
                if (obj.next().prop('checked')) {
                        obj.next().next().find('input:checkbox').prop('checked', true);
                } else {
                    if(!str111) {
                        $('input[name="checkall"]').prop('checked', false);
                        obj.next().next().find('input:checkbox').prop('checked', false);
                        }
                }
            }
        })


    });

    //关闭展开城市
    $(document).on('click', '.area_hidden', function () {
        var choose_id = $(this).next().val();
        $(".sec" + choose_id).empty();
        $(".last" + choose_id).empty();
        var obj = $(this);
        obj.attr("class", "select_choose dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon");
    });

    //点击地区复选框
    $(document).on('click', 'input:checkbox', function () {
        if ($(this).attr('area_deep') == 2) {
            if ($(this).prop('checked')) {
                $(this).next().find('input:checkbox').prop('checked', true);
            } else {
                $('input[name="checkall"]').prop('checked', false);
                //子集
                $(this).next().find('input:checkbox').prop('checked', false);
                //顶级
                $(this).parent('div').parent('div').find('input:checkbox:first').prop('checked', false);
            }
        } else if ($(this).attr('area_deep') == 1) {
            if ($(this).prop('checked')) {
                $(this).parent('div').find('input:checkbox').prop('checked', true);
            } else {
                $('input[name="checkall"]').prop('checked', false);
                $(this).parent('div').find('input:checkbox').prop('checked', false);
            }

        } else {
            if (!$(this).prop('checked')) {
                $('input[name="checkall"]').prop('checked', false);
                $(this).parent('div').parent('div').find('input:checkbox').first().prop('checked', false);
                $(this).parent('div').parent('div').parent('div').find('input:checkbox').first().prop('checked', false);
            }

        }
    });


    $('#button_submit').click(function(){
        var chk_values = [];
        var data_names = [];
        $('input[type="checkbox"]:checked').each(function () {
            chk_values.push($(this).attr('ids'));
            data_names.push($(this).attr('data_name'));
        });
        var area_name = unique(data_names);
        var chk_value = unique(chk_values);
        // alert()
        var url =ADMINSITEURL+'/Ziwork/updateArea';
        $.ajax({
            type: "POST",
            url: url,
            data:{workid:{:$workid},area_name:area_name,area_ids:chk_value},
            dataType: "json",
            error: function (request) {
                // alert("Connection error");
                alert('请选择至少一个区域');
            },
            success: function(result) {
                if (result.code == 0) {
                    layer.closeAll();
                    layer.msg('分配成功');
                    parent.location.reload();
                    return false;
                } else {
                    layer.closeAll();
                    layer.msg('分配失败');
                    parent.location.reload();
                    return false;
                }
            }
        });
    });


</script>